package app

import (
	"log"
	"reflect"
)

// GetIder Id获取器.
type GetIder interface {
	GetId() int32
}

func checkGetIders(i interface{}, name string) {
	itemExists := struct{}{}

	sv := reflect.ValueOf(i)
	if sv.Kind() != reflect.Slice {
		log.Fatal("参数不是 Slice")
	}

	getIDs := make([]GetIder, sv.Len())

	s := reflect.ValueOf(i)
	for i := 0; i < s.Len(); i++ {
		ele := s.Index(i)
		getIDs[i] = ele.Interface().(GetIder)
	}

	set := map[int32]struct{}{}

	for _, o := range getIDs {
		if _, has := set[o.GetId()]; has {
			log.Fatalf("%s Id: %d 重复", name, o.GetId())
		}

		set[o.GetId()] = itemExists
	}
}
